//
//  UIScrollView+HTRefresh.m
//
//  Copyright (c) 2015年 Risenb App Department With iOS. All rights reserved.
//

#import "UIScrollView+HTRefresh.h"
#import "ZPRefreshGifHeader.h"
#import "MJCustomAutoFooter.h"

@implementation UITableView (LDTableView)
- (void)setNotEstimatedRowHeight {
    self.estimatedRowHeight = 0;
    self.estimatedSectionHeaderHeight = 0;
    self.estimatedSectionFooterHeight = 0;
}
@end
@implementation UIScrollView (HTRefresh)
- (void)addLegendFooterTitleStr:(NSString *)str ChangeViewBlock:(void (^)(void))block {
    WEAK
      MJCustomAutoFooter *header = [MJCustomAutoFooter footerWithRefreshingBlock:^{
          if (block) {
              block();
          }
          STRONG;
          [self.mj_footer endRefreshing];
      }];
      header.footerReloadStr = str;
      header.automaticallyRefresh = NO;
      // 设置header
      self.mj_footer = header;
}
- (void)addLegendFooterChangeViewBlock:(void (^)(void))block {
    [self addLegendFooterTitleStr:nil ChangeViewBlock:block];
}
- (void)addLegendHeaderWithRefreshingBlock:(void (^)(void))block {
//    ZPRefreshGifHeader *header = [ZPRefreshGifHeader headerWithRefreshingBlock:^{
//        if(block)
//        {
//            block();
//        }
//    }];
//    NSMutableArray *refresh = [NSMutableArray array];
//    for (int i = 0; i < 8; i++) {
//        NSString *temp = [NSString stringWithFormat:@"refresh_loading_%d", i];
//        [refresh addObject:kImage(temp)];
//    }
//    [header setImages:refresh forState:MJRefreshStateIdle];
//
//    NSMutableArray *pull = [NSMutableArray array];
//    for (int i = 0; i < 23; i++) {
//        NSString *temp = [NSString stringWithFormat:@"refresh_juhua_%d", i];
//        [pull addObject:kImage(temp)];
//    }
//    [header setImages:pull forState:MJRefreshStatePulling];
//    [header setImages:pull forState:MJRefreshStateRefreshing];
//    header.y -= 20;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (block) {
            block();
        }
    }];
    self.mj_header = header;
}

- (void)addVideoHeadWithBlock:(void (^)(void))block {
    MJRefreshHeader *head = [MJRefreshHeader headerWithRefreshingBlock:^{
        if (block) {
            block();
        }
    }];
    self.mj_header = head;
}

- (void)addLegendHeaderWithNoArrowChangeViewBlock:(void (^)(void))block {
    
    self.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        if(block)
        {
            block();
        }
    }];
    MJRefreshStateHeader *header = (MJRefreshStateHeader *)self.mj_header;
    [header setTitle:@"下拉显示商品详情" forState:MJRefreshStateIdle];
    [header setTitle:@"释放显示商品详情" forState:MJRefreshStatePulling];
    [header setTitle:@"释放显示商品详情" forState:MJRefreshStateRefreshing];
    header.lastUpdatedTimeLabel.hidden = YES;

}
- (void)addLegendFooterWithRefreshingBlock:(void (^)(void))block {
    self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if(block)
        {
            block();
        }
    }];
    MJRefreshBackNormalFooter *header = (MJRefreshBackNormalFooter *)self.mj_footer;
    [header setTitle:@"没有更多数据了" forState:MJRefreshStateNoMoreData];
    self.mj_footer.hidden = NO;

}
- (void)addAutoFooterWithRefreshingBlock:(void (^)(void))block{
    self.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        if(block)
        {
            block();
        }
    }];
    MJRefreshAutoFooter *mj_footer =(MJRefreshAutoFooter *)self.mj_footer;
    
    mj_footer.triggerAutomaticallyRefreshPercent = -20;
    self.mj_footer.hidden = YES;

}
- (void)pullDownBlock:(void (^)(void))block
{
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if(block)
        {
            block();
        }
    }];
    
//    [self addGifHeaderWithRefreshingBlock:^{
//        if(block)
//        {
//            block();
//        }
//    }];
//
//    NSMutableArray *idleImages = [NSMutableArray array];
//    for (NSUInteger i = 1; i <= 60; i++)
//    {
//        NSString *imageString = [NSString stringWithFormat:@"dropdown_anim__000%zd", i];
//        UIImage *image = [UIImage imageNamed:MJRefreshSrcName(imageString)];
//        [idleImages addObject:image];
//    }
//    [self.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle];
//    
//    NSMutableArray *refreshingImages = [NSMutableArray array];
//    for (NSUInteger i = 1; i<=3; i++)
//    {
//        NSString *imageString = [NSString stringWithFormat:@"dropdown_loading_0%zd", i];
//        UIImage *image = [UIImage imageNamed:MJRefreshSrcName(imageString)];
//        [refreshingImages addObject:image];
//    }
//    [self.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStatePulling];
//    
//    [self.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStateRefreshing];
}

- (void)pullUpBlock:(void (^)(void))block
{
    self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if(block)
        {
            block();
        }
    }];
    self.mj_footer.hidden = YES;
}

- (void)startPullDown {
    [self.mj_header beginRefreshing];

}
- (void)startPullUp {
    [self.mj_footer beginRefreshing];

}
- (void)endPullDown
{
    [self.mj_header endRefreshing];
}

- (void)endPullUp
{
    [self.mj_footer endRefreshing];
}

- (void)hideFooter {
    self.mj_footer.hidden = YES;
}

-(void)footerHideHint{
    MJRefreshBackNormalFooter *header = (MJRefreshBackNormalFooter *)self.mj_footer;
    [header setTitle:@"" forState:MJRefreshStateRefreshing];
    [header setTitle:@"" forState:MJRefreshStateIdle];
    [header setTitle:@"" forState:MJRefreshStatePulling];
    [header setTitle:@"" forState:MJRefreshStateRefreshing];
}

-(void)liveLoadFinishHint{
    MJRefreshAutoNormalFooter *footer = (MJRefreshAutoNormalFooter *)self.mj_footer;
    footer.hidden = NO;
    [footer setTitle:@"仅显示最近两个月的记录" forState:MJRefreshStateNoMoreData];

}

- (void)isShowFooterNoMore:(NSArray *)data {
    if (data==nil || data.count == 0) {
        //        [self hideFooter];
        [self nomoreData];
    }else{
        [self showFooter];
        [self endPullUp];
    }
}

- (void)isShowFooter:(NSArray *)data count:(NSInteger)count {
    if (data==nil || data.count == 0 || data.count < count) {
        [self hideFooter];
    }else{
        [self showFooter];
    }
    
}
- (void)isShowFooter:(NSArray *)data {
    if (data==nil || data.count == 0) {
        [self hideFooter];
    }else{
        [self showFooter];
    }
}
- (BOOL)isRefresh {
    if (self.mj_header.isRefreshing || self.mj_footer.isRefreshing) {
        return YES;
    }
    return NO;
}
- (void)showFooter{
    self.mj_footer.hidden = NO;
}
- (void)nomoreData
{
    [self showFooter];
    [self.mj_footer endRefreshingWithNoMoreData];
}
@end
@implementation LDTableView

- (void)reloadData {
    [super reloadData];
    if ([self.dataSource respondsToSelector:@selector(tableView:numberOfRowsInSection:)]) {
        NSInteger count = [self.dataSource tableView:self numberOfRowsInSection:0];
        if (count == 0) {
            [self hideFooter];
        }
    }
}
@end



